Big Tree
The "Big Three" List Operations Overview:
In Scheme (and Scamper), there are three key higher-order list operations: map, apply, and reduce (also called fold). These operations are powerful tools for manipulating lists and allow for both repetition and parallelism.
1. map
Function:
- Purpose: The
map
function applies a given procedure to each element of a list and returns a new list with the results. - Key Idea: Instead of using loops (like in other programming languages),
map
allows for a different way of thinking about repetition by applying the same function to each element in parallel.
Example of map
:
(map (lambda (x) (* x 2)) (list 1 2 3 4)) ; produces (2 4 6 8)
- Benefits:
- Parallel Processing: The function can be applied to multiple elements simultaneously, which is crucial in modern computing for speed (e.g., in Google and Amazon's systems).
- Encourages decomposition: You don’t need to think about the whole list, just the operation on a single element.
2. apply
Function:
- Purpose:
apply
takes a procedure and applies it to a list of arguments, combining the elements of the list into a single value. - Key Idea:
apply
is useful for applying functions that can take an arbitrary number of arguments (like+
or*
) to a list.
Example of apply
:
(apply + (list 1 2 3 4 5)) ; produces 15
apply
is great for reducing lists into a single value when you have a function that can take multiple inputs.
3. reduce
(or fold
) Function:
- Purpose:
reduce
combines neighboring elements of a list, using a function to repeatedly combine pairs until a single value remains. - Key Idea: It’s useful when you need to accumulate results over a list but don’t have a function that works with multiple inputs (like
string-append
).
Example of reduce
:
(reduce + (list 1 2 3 4)) ; produces 10
- In string manipulations:
(reduce string-append (list "a" "b" "c")) ; produces "abc"
Combining Lists with Spaces:
- Consider reversing the words in a string and then combining them back with spaces:
(reduce (section string-append _ " " _) (map (o list->string reverse string->list) (string-split "this is a test"))) ; produces "siht si a tset"
Associativity and Efficiency:
-
Associativity: Operations like addition, multiplication, and
string-append
are associative, meaning it doesn’t matter in what order the operations are performed.- This allows
reduce
to operate efficiently in parallel.
- This allows
-
Order of Operations:
- Not all operations are associative. For example, subtraction produces different results depending on the order of operations. To support different orders, Scamper provides both
reduce
(left-to-right) andreduce-right
(right-to-left).
- Not all operations are associative. For example, subtraction produces different results depending on the order of operations. To support different orders, Scamper provides both
4. filter
Function:
- Purpose:
filter
selects elements from a list that satisfy a predicate function (i.e., a function that returns true or false).
Example of filter
:
(filter odd? (list 1 2 3 4 5)) ; produces (1 3 5)
- Combining Functions: You can combine
filter
,map
, andreduce
for more complex operations. For example, filtering digits from a string, mapping them to numbers, and summing them:(reduce + (map char->integer (filter char-numeric? (string->list "123abc"))))
Using map
with Multiple Lists:
- Key Idea:
map
can also apply a function to corresponding elements from multiple lists, combining them into a single list.
Example:
(map + (list 1 2 3) (list 4 5 6)) ; produces (5 7 9)
Self-Check Questions:
-
Check 1: Inconsistent Subtraction:
- Find different results for the expression
(4 - 1 - 6 - 3 - 2 - 10 - 8)
using different orders of operation.
- Find different results for the expression
-
Check 2: Pipelining (‡):
- Extend an expression using
map
,filter
, andreduce
so that:- The list only contains names in "last, first" format with at least 13 characters.
- The sum of the lengths of all these names is computed.
- Extend an expression using